from mitmproxy import http

NEW_HOST = "localhost"
NEW_PORT = 1234
NEW_SCHEME = "http"

class Redirector:
    def request(self, flow: http.HTTPFlow) -> None:
        pretty_url = flow.request.pretty_url
        flow.request.host = NEW_HOST
        flow.request.port = NEW_PORT
        flow.request.scheme = NEW_SCHEME
        print(f"Redirected {pretty_url} to -> {NEW_SCHEME}://{NEW_HOST}:{NEW_PORT}")
    def http_connect(self, flow: http.HTTPFlow) -> None:
        flow.server_conn.address = (NEW_HOST, NEW_PORT)
        print(f"CONNECT intercept: Rewrote server address to {NEW_HOST}:{NEW_PORT}")

addons = [Redirector()]
